#include<func.h>
#include"head.h"
#include"circleQueue.h"

int rm(MYSQL *db, char *argv, char *pwd, char *usr_name, int netFd){
    //rm ./dir1/file1
    //rm /xxx/file1
    //rm file1
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[1024] = {0};

    if(filePath[0] == '.'){
        if(filePath[1] == '/'){ // rm ./
            sprintf(realPath, "%s%s", pwd, filePath + 1);
        }
        else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }
    else if(filePath[0] == '/'){
        strcpy(realPath, filePath);
    }
    else{  //rm file
        sprintf(realPath, "%s/%s", pwd, filePath);
    }
    //printf("realPath = %s\n", realPath);
    
    //判断是否有此路径
    int ret = isFileAndIsBlongToUsr(db, realPath, usr_name);
    if(ret == -1){
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
    else{
        send(netFd, "0", 1, MSG_NOSIGNAL);
        delCatalogByFilename(db, realPath);
        return 0;
    }
}

/*cd还需优化功能：
    1. cd ..
    2. 禁止用户cd到其他用户目录下*/
int cd(MYSQL *db, char *argv, char *pwd, char *usr_name, int netFd){
    // cd ..
    //cd ./dir1
    //cd /xxx/xxx
    //cd xxx
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[1024] = {0};

    if(filePath[0] == '.'){
        if(filePath[1] == '.'){  //cd ..
            //sprintf();
            //child_file_path pwd
            int ret = FindFatherPathByChildPath(db, pwd, realPath);
            //printf("realPath = %s\n", realPath);
            if(ret == -1){
                send(netFd, "1", 1, MSG_NOSIGNAL);
                return -1;
            }
        }
        else if(filePath[1] == '/'){ // cd ./
            sprintf(realPath, "%s%s", pwd, filePath + 1);
        }
        else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }
    else if(filePath[0] == '/'){
        strcpy(realPath, filePath);
    }
    else{  //cd file
        sprintf(realPath, "%s/%s", pwd, filePath);
    }
    //printf("realPath = %s\n", realPath);
    
    //判断是否有此路径
    int ret = isDirAndIsBlongToUsr(db, realPath, usr_name);
    if(ret == -1){
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
    else{
        send(netFd, "0", 1, MSG_NOSIGNAL);
        memset(pwd, 0, sizeof(pwd));
        strcpy(pwd, realPath);
        return 0;
    }
}

int ls(MYSQL *db, char *pwd, int netFd){
    //pwd /usr1
    int pre_id = getIdFromFileName(db, pwd);
    char arr[1024] = {0};
    int ret = findChildFileAndDirFromPreId(db, pre_id, arr);
    if(ret == -1){
        send(netFd, "0", 1, MSG_NOSIGNAL);
        return 0;
    }
    send(netFd, "1", 1, MSG_NOSIGNAL);
    train_t train;
    train.dataLength = strlen(arr);
    strcpy(train.buf, arr);
    send(netFd, &train, sizeof(train.dataLength) + train.dataLength, MSG_NOSIGNAL);
    return 0;
}

int pwd(char *pwd, int netFd){
    train_t train;
    strcpy(train.buf, pwd);
    train.dataLength = strlen(train.buf);
    send(netFd, &train, sizeof(train.dataLength) + train.dataLength, MSG_NOSIGNAL);
    return 0;
}

int makedir(MYSQL *db, char *argv,char *pwd, char *usr_name, int netFd){
    //mkdir file1
    //pwd /usr1/dir1
    char arr[100] = "";
    strcpy(arr, argv);
    char *command = strtok(arr, " ");
    char *fileName = strtok(NULL, " ");

    char newFileName[100] = {0};
    sprintf(newFileName,"%s/%s", pwd, fileName);
    int pre_id = getIdFromFileName(db, pwd);
    int ret = insertDirToCatalog(db, newFileName, usr_name, pre_id);
    if(ret == -1){
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
    send(netFd, "0", 1, MSG_NOSIGNAL);
    return 0;
}

int removedir(MYSQL *db, char *argv, char* pwd, char* usr_name, int netFd){
    //rmdir ./xxx/
    //rmdir /ddd
    //rmdir xxx
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[1024] = {0};

    if(filePath[0] == '.'){
        if(filePath[1] == '/'){ // rmdir ./
            sprintf(realPath, "%s%s", pwd, filePath + 1);
        }
        else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }
    else if(filePath[0] == '/'){
        strcpy(realPath, filePath);
    }
    else{  //rmdir file
        sprintf(realPath, "%s/%s", pwd, filePath);
    }
    //printf("realPath = %s\n", realPath);
    
    //判断是否有此路径
    int ret = isDirCanDelete(db, realPath, usr_name);
    if(ret == -1){
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
    else{
        delDirFromFilename(db, realPath);
        send(netFd, "0", 1, MSG_NOSIGNAL);
        return 0;
    }
}

int delDirFromFilename(MYSQL *db, char *file_name){
    idQueue_t* pIdQueue = (idQueue_t *)calloc(1, sizeof(idQueue_t));
    initIdQueue(pIdQueue);
    int id = getIdFromFileName(db, file_name);
    idEnQueue(pIdQueue, id);
    int pre_id;
    while(pIdQueue->queueSize != 0){
        pre_id = idDeQueue(pIdQueue);
        delCatalogById(db, pre_id);
        //select id from Catalog where pre_id = xxx;
        char sqlCommand[255] = {0};
        sprintf(sqlCommand, "select id from Catalog where pre_id = %d;", pre_id);
        int qret = mysql_query(db, sqlCommand);
        if(qret != 0){
            //printf("Error: %s\n", mysql_error(db));
            return -1;
        }
        MYSQL_RES *result = mysql_store_result(db);
        MYSQL_ROW row = mysql_fetch_row(result);
        if(row == NULL){
            continue;
        }
        else{
            do{
                for(int i = 0; i < mysql_num_fields(result); ++i){
                    int id = atoi(row[0]);
                    idEnQueue(pIdQueue, id);
                }
            }while((row = mysql_fetch_row(result)) != NULL);
        }
    }

}
//getIdFromFileName(MYSQL *db, char* file_name)